# == Schema Information
#
# Table name: events
#
#  id         :integer          not null, primary key
#  name       :string(255)
#  created_at :datetime
#  updated_at :datetime
#  show_time  :datetime
#

class Event < ActiveRecord::Base
  has_many :duels, dependent: :destroy

  validates :name, presence: true
  validates :show_time, presence: true

  def finished?
    self.show_time < DateTime.now
  end

  def gen_duels(dummy=false)
    teams = Team.where(['survived = true']).to_a
    if dummy
      teams.each do |team|
        self.duels.create!(a_id: team.id, b_id: 0)
      end
    else
      if teams.count % 2 == 0
        teams.shuffle!
        pairs=teams.each_slice(2).to_a
        pairs.each do |pair|
          self.duels.create!(a_id: pair[0].id, b_id: pair[1].id)
        end
      end
    end
  end
end
